package cn.simon.controller;

import cn.simon.model.Cart;
import cn.simon.service.CartService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Kinfer
 * @date 2022/10/31
 */
@Log
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 显示购物车所有商品
     *
     * @param model
     * @return
     * @author Kinfer
     * @date 2022/10/31
     */
    @GetMapping("/showCart")
    public String showCart(Model model) {
        // TODO: 2022/10/31 需要获取用户id
        Integer userId = 1;

        List<Cart> cartList = cartService.getCart(userId);
        model.addAttribute(cartList);

        return "cart";
    }

    /**
     * 删除购物车内指定商品
     *
     * @param cartId
     * @return
     * @author Kinfer
     * @date 2022/10/31
     */
    @GetMapping("/delCart")
    public String delCart(Integer cartId) {
        cartService.delCart(cartId);
        return "redirect:showCart";
    }

    /**
     * 清空购物车
     *
     * @return
     * @author Kinfer
     * @date 2022/10/31
     */
    @GetMapping("/delAllCart")
    public String delAllCart() {
        // TODO: 2022/10/31 获取userId
        Integer userId = 1;

        cartService.delAllCart(userId);

        return "redirect:showCart";
    }

    @GetMapping("/addOrderFromCart")
    public String addOrderFromCart(Integer[] cartId){
        
        return null;
    }
}
